package com.wwwarehouse.common.custom_widget.share;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.custom_widget.popupwindow.EasyPopupWindow;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.common.tools.ToastUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * author ：minifly
 * date: 2017/8/18
 * time: 09:49
 * desc: 分享弹出的popuwindow 里面做内容完整性及显示类型判断
 */
public class SharePopuwindow implements EasyPopupWindow.ChildClickListener, View.OnClickListener {

    private List<ShareItem> list;
    private GridView gridView;
    private OnSheetItemClickListener listener;


    private static SharePopuwindow popuwindow = null;
    private Context mContext;
    private ShareContent content;
    private Dialog mDialog;
    private LinearLayout bottomLin;
    private TextView bottomBtn;
    private boolean isShowCancleBtn = false;
    private EasyPopupWindow easyPopupWindow;
    private GridViewAdapter gridViewAdapter;

    public static SharePopuwindow getInstance(Context context) {
//        if(popuwindow == null){
        popuwindow = new SharePopuwindow(context);//每次的context需要更新，所以不能单例的来了。
//        }
        return popuwindow;
    }

    public SharePopuwindow(Context context) {
        this.mContext = context;
    }

    @Override
    public final void getChildView(int layoutId, View view) {
        gridView = (GridView) view.findViewById(R.id.bottom_gridview_id);
        bottomLin = (LinearLayout) view.findViewById(R.id.share_bottom_btn_lin);
        bottomBtn = (TextView) view.findViewById(R.id.share_bottom_btn_id);

        if(!isShowCancleBtn){
            bottomLin.setVisibility(View.GONE);
        }else{
            bottomLin.setVisibility(View.VISIBLE);
            bottomLin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    easyPopupWindow.dismiss();
                }
            });
        }
        gridViewAdapter = new GridViewAdapter(list,mContext);
        gridView.setAdapter(gridViewAdapter);
        gridView.setNumColumns(4);//设置gridview的列数
    }

    public SharePopuwindow showCancleBtn(boolean isShowCancleBtn){
        this.isShowCancleBtn = isShowCancleBtn;
        return popuwindow;
    }

    /**
     * 图片资源id   选项字符串 tag
     */
    public SharePopuwindow addItem(int imgRes, String title, Object tag,boolean showProgress){
        if(list == null){
            list = new LinkedList<>();
        }
        list.add(new ShareItem(imgRes,title,tag,showProgress));
        return popuwindow;
    }
    /**
     * 图片资源id   选项字符串 tag
     * @param imgRes
     * @param title
     * @param tag
     */
    public SharePopuwindow addItem(int imgRes, String title, Object tag){
        if(list == null){
            list = new LinkedList<>();
        }
        list.add(new ShareItem(imgRes,title,tag));
        return popuwindow;
    }

    /**
     * 设置一个tag的loading形式
     */
    public SharePopuwindow changeShowType(Object tag,ShareItem.TypeState typeState){
        for(ShareItem item : list){
            if(item.getTag().equals(tag)){
                item.setTypeState(typeState);
                break;
            }
        }
        gridViewAdapter.notifyDataSetChanged();
        return popuwindow;
    }

    /**
     * 设置监听
     */
    public SharePopuwindow setOnSheetItemClickListener(OnSheetItemClickListener listener){
        this.listener = listener;
        return popuwindow;
    }

    @Override
    public final void onClick(View v) {
        if (content == null) {
            ToastUtils.showToast(R.string.share_share_needcontent);
            return;
        }
    }

    public SharePopuwindow setContent(ShareContent content) {
        this.content = content;
        return popuwindow;
    }
    /**
     * 显示popwindow
     */
    public void showPop() {
        easyPopupWindow = PopUpUtils.showPopUpBottom(R.layout.share_bottom_layout, mContext, ((Activity) mContext).findViewById(android.R.id.content), true, this);
    }

    public  void hidePop(){
        if(easyPopupWindow!=null && easyPopupWindow.isShowing()){
            easyPopupWindow.dismiss();
        }
    }

    class GridViewAdapter extends BaseAdapter{
        List<ShareItem> list ;
        Context mContext;

        public GridViewAdapter(List<ShareItem> list,Context mcontext){
            this.list = list;
            this.mContext = mcontext;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            GridViewHoder holder = null;
            final ShareItem item = list.get(position);
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.share_item_content, null);
                holder = new GridViewHoder(convertView);
                holder.shareTxt.setText(item.getTitle());
                holder.shareImg.setImageResource(item.getImageRes());
                convertView.setTag(holder);
            } else {
                holder = (GridViewHoder) convertView.getTag();
            }

            holder.lin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(popuwindow,item.getTag());
                }
            });
            holder.loadingRel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });

            switch (item.getTypeState()){
                case CONTENT:
                    holder.lin.setVisibility(View.VISIBLE);
                    holder.shareTxt.setVisibility(View.VISIBLE);
                    holder.shareImg.setVisibility(View.VISIBLE);
                    holder.loadingRel.setVisibility(View.GONE);
                    holder.loadingImg.setVisibility(View.GONE);
                    holder.loadingImg.clearAnimation();
                    break;
                case LOADING:
                    holder.shareTxt.setVisibility(View.GONE);
                    holder.shareImg.setVisibility(View.GONE);
                    holder.lin.setVisibility(View.GONE);
                    holder.loadingRel.setVisibility(View.VISIBLE);
                    holder.loadingImg.setVisibility(View.VISIBLE);
                    if (holder.loadingImg != null) {
                        Animation circle_anim = AnimationUtils.loadAnimation(mContext, R.anim.anim_round_rotate);
                        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
                        circle_anim.setInterpolator(interpolator);
                        if (circle_anim != null) {
                            holder.loadingImg.startAnimation(circle_anim);  //开始动画
                        }
                    }
                    break;
                case NONE:
                    holder.shareTxt.setVisibility(View.GONE);
                    holder.shareImg.setVisibility(View.GONE);
                    holder.loadingImg.setVisibility(View.GONE);
                    holder.loadingRel.setVisibility(View.GONE);
                    holder.lin.setVisibility(View.GONE);
                    holder.loadingImg.clearAnimation();
                    break;
            }

            return convertView;
        }

        class GridViewHoder{
            RelativeLayout lin;
            ImageView shareImg;
            TextView shareTxt;
            ImageView loadingImg;
            RelativeLayout loadingRel;

            public GridViewHoder(View view){
                lin = (RelativeLayout) view.findViewById(R.id.share_lin);
                loadingRel = (RelativeLayout) view.findViewById(R.id.share_lin_loading);
                loadingImg = (ImageView) view.findViewById(R.id.share_content_loading);
                shareImg = (ImageView) view.findViewById(R.id.share_content_img);
                shareTxt = (TextView) view.findViewById(R.id.share_content_txt);
            }
        }

    }

    public interface OnSheetItemClickListener {
        void onClick(SharePopuwindow popupWindow ,Object tag);
    }

}
